﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.IO;
using System.Timers;
using System.Windows.Threading;
using System.Threading;

namespace SYcwpb.Controls
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class CtlDutyInfo : UserControl
    {
        /// <summary>
        /// 迟到分等级的时间
        /// </summary>
        private BitmapImage myBitmapImg;
        //private BitmapEffectGroup myBitmapEffectGroup = new BitmapEffectGroup();
        private DispatcherTimer effctTimer;

        /// <summary>
        /// 用于检查当前时间是否大于出勤时间，是的话，Warn
        /// </summary>
        //private Thread CheckDutyTimeThread = null;


#region 图片拉伸效果参数
        private const double scaleX = 0.96;
        private const double scaleY = 0.96;
        private double centerX = 50;
        private double centerY = 225;
#endregion

#region 阴影效果参数
        private DropShadowBitmapEffect myDropShadowBitmapEffect;
        private Color DropShadowColor = Colors.Black;
        private const int DropShadowDirection = 320;
        private const int DropShadowShadowDepth = 8;
        private const int DropShadowSoftness = 1;
        private const double DropShadowOpacity = 1;

#endregion

#region 发光效果参数设置
        private OuterGlowBitmapEffect myOuterGlowEffect ;
        private const double myGlowSize = 8;
        private const double myGlowNoise = 0;
        private const double myGlowOpacity = 1;
        private Color myGlowColor = Colors.YellowGreen;
#endregion
        private const double timerInerval = 1;


        private string _staffName;
        private string _dutyTime;
        private string _phone;
        private string _checi;
        private string _imageUri;
        private bool _warn = false;
        private bool _hasEffct;           //图像是否添加了特效
        private bool _imgNormal = true;   //图像是否为正常大小

        public CtlDutyInfo()
        {
            InitializeComponent();

            initTimer();
            setDropShadowEffct();
            setOuterGlowEffct();

            //CheckDutyTimeThread = new Thread(new ThreadStart(CheckTime));

            //正常情况下，设置ToolTip不可见，只有提醒出勤状态下才可见
            imgToolTip.Visibility = Visibility.Hidden;
        }

        private void initTimer()
        {
            effctTimer = new DispatcherTimer ();
            effctTimer.Interval = TimeSpan.FromSeconds(timerInerval);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
           // effctTimer.Tick += new EventHandler(showWarnEffect);
        }

        //拉伸图片
        private void stretchImage()
        {
            setScaleCenter();
            imgScaleTransForm.ScaleX = imgScaleTransForm.ScaleX * scaleX;
            imgScaleTransForm.ScaleY = imgScaleTransForm.ScaleY * scaleY;
        }

        //还原
        private void restoreImage()
        {
            setScaleCenter();
            imgScaleTransForm.ScaleX = 1;
            imgScaleTransForm.ScaleY = 1;
        }

        //设置伸缩中心
        private void setScaleCenter()
        {
            imgScaleTransForm.CenterX = centerX;
            imgScaleTransForm.CenterY = centerY;

        }

        //显示图片效果
        private void showWarnEffect(object sender, EventArgs e)
        {
            //if (!_hasEffct)
            //{
            //    //myBitmapEffectGroup.Children.Add(myDropShadowBitmapEffect);
                
            //    myBitmapEffectGroup.Children.Add(myOuterGlowEffect);
            //    myBitmapEffectGroup.Children.Add(myDropShadowBitmapEffect);
            //    _hasEffct = true;
            //}
           //if (ImageUri == null) { effctTimer.Stop(); return; }
           // if (_imgNormal)
           // {
           //     //img.BitmapEffect = myBitmapEffectGroup;
           //     stretchImage();
           //     _imgNormal = false;
           // }
           // else
           // {
           //     restoreImage();
           //     //img.BitmapEffect = null;
           //     _imgNormal = true;
           // }
        }

        public bool Warn
        {
            get { return _warn; }
            set 
            { 
                _warn = value; 
                if (_warn)
                {
                    imgToolTip.Visibility = Visibility.Visible;
                    effctTimer.Start();
                }
                else
                {
                    //img.BitmapEffect = null;
                    restoreImage();
                    _imgNormal = true;
                    imgToolTip.Visibility = Visibility.Hidden;
                    effctTimer.Stop();
                }
            }
        }

        public string ImageUri
        {
            get { return _imageUri; }
            set
            {
                _imageUri = value;
                setImage();
            }
        }

        private void setImage()
        {
            if (_imageUri == null)
            {
                img.Source = null;
                return;
            }
            myBitmapImg = new BitmapImage(new Uri(_imageUri));
            myBitmapImg.DecodePixelWidth = 100;
            img.Source = myBitmapImg;
        }

        private void setDropShadowEffct()
        {
            myDropShadowBitmapEffect = new DropShadowBitmapEffect();
            myDropShadowBitmapEffect.Color = DropShadowColor;
            myDropShadowBitmapEffect.Direction = DropShadowDirection;
            myDropShadowBitmapEffect.ShadowDepth = DropShadowShadowDepth;
            myDropShadowBitmapEffect.Softness = DropShadowSoftness;
            myDropShadowBitmapEffect.Opacity = DropShadowOpacity;
        }

        private void setOuterGlowEffct()
        {
            myOuterGlowEffect = new OuterGlowBitmapEffect();
            myOuterGlowEffect.GlowColor = myGlowColor;
            myOuterGlowEffect.GlowSize = myGlowSize;
            myOuterGlowEffect.Noise = myGlowNoise;
            myOuterGlowEffect.Opacity = myGlowOpacity;
        }

        public string StaffName
        {
            get { return _staffName; }
            set
            {
                _staffName = value;
                labName.Text = _staffName;
            }
        }

        public string DutyTime
        {
            get { return _dutyTime; }
            set
            {
                _dutyTime = value;
                labTime.Text = _dutyTime;
            }
        }

        public string Phone
        {
            get { return _phone; }
            set
            {
                _phone = value;
                labPhone.Text = _phone;
            }
        }

        public string Checi
        {
            get { return _checi; }
            set
            {
                _checi = value;
                labCheci.Text = _checi;
            }
        }

        public string ScaleToString()
        {
            string result;
            result = imgScaleTransForm.ScaleX.ToString() + "  ;" + imgScaleTransForm.ScaleY.ToString();
            return result;
        }

        private void image_MouseLeftDown(object sender, MouseButtonEventArgs e)
        {
            
            if (e.ClickCount == 2 && effctTimer.IsEnabled)
            {
                this.Warn = false;
            }
        }
    }
    
}
